<?php

namespace App\Http\Controllers;

use App\Jobs\SendReminderEmail;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Mail\Mailer;
use Illuminate\Support\Facades\Mail;
use Maatwebsite\Excel\Facades\Excel;

class JobTestController extends Controller
{
    //

    public function addJob()
    {
//        $job = (new SendReminderEmail())->delay(300);
        $job = (new SendReminderEmail());
        $res = $this->dispatch($job);
        dump($res);
    }

    public function sendMail(Mailer $mailer)
    {

        $table_title = ['课程分校', '年份', '课程名称', '开讲时间', '主讲老师', '讲义名称', '讲义编号', '对应讲次', '讲义备注', '讲义分校', '寄送放', '预计邮寄时间'];
        $row_data = [];
        $list[] = [
            'branch_code_name'=>'北京分校',
            'year'=>'2018',
            'course_name'=>'课程名称',
            'speaker_time'=>'2018-02',
            'speaker_teacher'=>'小张',
            'handouts_info' => [
                [
                    'name'=>'讲义名称1',
                    'short_code'=>'A0001',
                    'chapter_number'=>'x0000212',
                    'remark'=>'备注',
                    'handouts_branch_code_name'=>'讲义北京分校',
                    'handouts_send_branch_code_name'=>'讲义寄送分校',
                    'predict_send_time'=>'2018-01-04',
                ],
                [
                    'name'=>'讲义名称2',
                    'short_code'=>'A0002',
                    'chapter_number'=>'x0000213',
                    'remark'=>'备注',
                    'handouts_branch_code_name'=>'讲义北京分校',
                    'handouts_send_branch_code_name'=>'讲义寄送分校',
                    'predict_send_time'=>'2018-01-04',
                ]
            ]
        ];



        $list[] = [
            'branch_code_name'=>'上海分校',
            'year'=>'2018',
            'course_name'=>'课程名称',
            'speaker_time'=>'2018-03',
            'speaker_teacher'=>'小张',
            'handouts_info' => [
                [
                    'name'=>'讲义名称1',
                    'short_code'=>'B0001',
                    'chapter_number'=>'Y0000212',
                    'remark'=>'备注',
                    'handouts_branch_code_name'=>'讲义北京分校',
                    'handouts_send_branch_code_name'=>'讲义寄送分校',
                    'predict_send_time'=>'2018-01-05',
                ],
                [
                    'name'=>'讲义名称2',
                    'short_code'=>'B0002',
                    'chapter_number'=>'Y0000213',
                    'remark'=>'备注',
                    'handouts_branch_code_name'=>'讲义北京分校',
                    'handouts_send_branch_code_name'=>'讲义寄送分校',
                    'predict_send_time'=>'2018-01-05',
                ],
                [
                    'name'=>'讲义名称3',
                    'short_code'=>'B0003',
                    'chapter_number'=>'Y0000213',
                    'remark'=>'备注',
                    'handouts_branch_code_name'=>'讲义北京分校',
                    'handouts_send_branch_code_name'=>'讲义寄送分校',
                    'predict_send_time'=>'2018-01-05',
                ]
            ]
        ];

        $list[] = [
            'branch_code_name'=>'北京分校',
            'year'=>'2018',
            'course_name'=>'课程名称',
            'speaker_time'=>'2018-02',
            'speaker_teacher'=>'小张',
            'handouts_info' => [
                [
                    'name'=>'讲义名称1',
                    'short_code'=>'A0001',
                    'chapter_number'=>'x0000212',
                    'remark'=>'备注',
                    'handouts_branch_code_name'=>'讲义北京分校',
                    'handouts_send_branch_code_name'=>'讲义寄送分校',
                    'predict_send_time'=>'2018-01-04',
                ],
                [
                    'name'=>'讲义名称2',
                    'short_code'=>'A0002',
                    'chapter_number'=>'x0000213',
                    'remark'=>'备注',
                    'handouts_branch_code_name'=>'讲义北京分校',
                    'handouts_send_branch_code_name'=>'讲义寄送分校',
                    'predict_send_time'=>'2018-01-04',
                ]
            ]
        ];

        foreach ($list as $key=>$info){
            $num = count($info['handouts_info']);
            foreach ($info['handouts_info'] as $k=>$handouts_info){
                $tmp_data = [
                    'branch_code_name' => $info['branch_code_name'],
                    'year' => $info['year'],
                    'course_name' => $info['course_name'],
                    'speaker_time' => $info['speaker_time'],
                    'speaker_teacher' => $info['speaker_teacher'],
                    'name' => $handouts_info['name'],
                    'short_code' => $handouts_info['short_code'],
                    'chapter_number' => $handouts_info['chapter_number'],
                    'remark' => $handouts_info['remark'],
                    'handouts_branch_code_name' => $handouts_info['handouts_branch_code_name'],
                    'handouts_send_branch_code_name' => $handouts_info['handouts_send_branch_code_name'],
                    'predict_send_time' => $handouts_info['predict_send_time'],
                ];
                if($k === 0){
                    $tmp_data['need_merge'] = $num;
                }
                $row_data[] = $tmp_data;
            }
        }

        $name = "讲次测试";
        $column_name_list = self::columnName();
        $file_name  = $name .'_'. date('YmdHis');
        $need_merge_row = ['branch_code_name', 'year', 'course_name', 'speaker_time', 'speaker_teacher'];

        Excel::create($file_name, function ($excel) use ($row_data, $need_merge_row, $column_name_list, $table_title) {
            $excel->sheet('sheet_name', function ($sheet) use ($row_data, $need_merge_row, $column_name_list, $table_title){

                $sheet->setAutoSize(true); //开启自动调整大小

                foreach ($row_data as $key=>$info) {
                    if(isset($info['need_merge'])){
                        $need_merge_num = $info['need_merge'];
                        unset($info['need_merge']);
                        $i = 0;
                        foreach ($info as $value_key=>$value){
                            if(in_array($value_key, $need_merge_row)){
                                $column_name = $column_name_list[$i];
                                $cell = $column_name . ($key + 1) . ":" . $column_name . ($key + $need_merge_num);
                                $sheet->mergeCells($cell);
                                $i++;
                            }
                        }
                    }
                    $sheet->row($key + 1, $info);        // 操作第一行
                }
                $sheet->prependRow($table_title); //插入表头

//                $sheet->setHeight(1, 50); //设置 整个行的高度

                $sheet->row(1, function($row){
                    $row->setBackground('#808080');     // 设置单元格背景
                });

            });

        })->export('xlsx');



//        $name = '学院君';
//        try{
//            $flag = $mailer->send('mail',['name'=>$name],function($message){
//                $to = 'lipeng9@100tal.com';
//                $message ->to($to)->subject('测试邮件');
//                $attachment = './favicon.ico';
//                //在邮件中上传附件
//                $message->attach($attachment);
//            });
//        } catch (\Exception $e){
//            dump($e->getMessage());
//        }

    }
    public static function  columnName($max_num = 50)
    {
        $single_name_list = [
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
            'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        ];
        $num = 0;
        $name_list = $single_name_list;
        for ($i = 0; $i <= 25; $i++){
            for ($j = 0; $j <= 25; $j++){
                $name_list[] = $single_name_list[$i] . $single_name_list[$j];
                $num++;
                if($num >= ($max_num - 26)){
                    break 2;
                }
            }
        }
        return $name_list;
    }
}
